fix(session): check for context overflow mid-turn in finish-step#6480
Merged
rekram1-node merged 3 commits intoanomalyco:devfrom Jan 1, 2026
Merged
Conversation
Previously, context compaction only triggered after an assistant turn completed. When a model made many tool calls with large outputs, context could overflow mid-turn before compaction had a chance to run. Now we check isOverflow using actual token counts from the AI SDK's finish-step event during streaming. If overflow is detected, we break the stream loop early and return 'compact' to trigger compaction immediately. - processor.ts: Check overflow in finish-step, break early, return 'compact' - prompt.ts: Handle 'compact' return, trigger SessionCompaction.create - Added integration tests for processor compaction behavior
ab8521e to
e74bd7c
Compare
Collaborator
|
/review |
2d73d4b to
4198f2a
Compare
triklozoid
pushed a commit
to triklozoid/opencode
that referenced
this pull request
Feb 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
finish-stepevent handler using actual token counts from AI SDK"compact"when overflow detected"compact"return in prompt.ts to triggerSessionCompaction.createimmediatelyProblem
Context compaction only triggered after an assistant turn completed. When a model made many tool calls with large outputs, context could overflow mid-turn before compaction had a chance to run.
Solution
Move the overflow check from post-turn to mid-turn by checking in the
finish-stepevent handler (which fires after each step). This uses actual token counts from the AI SDK rather than estimates.Before / After
Changes
isOverflowinfinish-step, setneedsCompactionflag, break loop early, return"compact""compact"return, triggerSessionCompaction.create, continue loopTesting
15 tests covering the compaction data flow:
session.compaction.isOverflow(5 tests) - overflow detection logicutil.token.estimate(3 tests) - token estimationsession.getUsage(7 tests) - AI SDK usage normalization to our token formatTests verify the key units without heavy mocking - if
getUsagecorrectly normalizes AI SDK tokens andisOverflowcorrectly detects overflow, the integration works.